<?php
@session_start();
//ini_set('display_errors', 1);
//ini_set('error_reporting', 1);


// Include the YOS library.
require 'lib/Yahoo.inc.php';

function echoDbg($what, $desc = '')
{
	if($desc)
		echo "<b>$desc:</b> ";
	echo "<pre>";
	print_r($what);
	echo "</pre>\n";
}

define('OAUTH_CONSUMER_KEY', 'dj0yJmk9dzczSjJoRTNWWEpJJmQ9WVdrOVdubEliMGQ0TTJVbWNHbzlNVFU1TXpnNE1URTJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD05Yg--'); // Place Yoru Consumer Key here
define('OAUTH_CONSUMER_SECRET', 'c622672ce5ca6ca45e1fb02f43f8b039c64c583c'); // Place your Consumer Secret
define('OAUTH_APP_ID', 'ZyHoGx3e'); // Place Your App ID here


if(isset($_GET['callback']) && $_GET['callback']) {
	if(!isset($_REQUEST['oauth_verifier'])){
		YahooSession::clearSession();
	}
	
	$_SESSION['callback'] = $_REQUEST['callback'];
}

if(isset($_GET['security_token']) && $_GET['security_token']) {
	$_SESSION['security_token'] =  $_GET['security_token'];
}
if(isset($_GET['token_name']) && $_GET['token_name']) {
    $_SESSION['token_name'] =  $_GET['token_name'];
}
$sSecurityToken =  @$_SESSION['security_token'];
$token_name =  @$_SESSION['token_name'];
if($token_name == "")
{
    $token_name = "phpfox[security_token]";
}


if(array_key_exists("login", $_REQUEST))
{
	
	$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
	//echoDbg($session);
	
	$sImage =  <<<EOF
	<div id="loader" style="text-align: center;">

		<img src="images/ajax-loader.gif"/>
		<br/>
		<a href="javascript:void();" onclick="submitYahoo();">Redirecting. If you can't wait - click here</a>
	</div>
EOF;
	if(is_object($session))
	{
		$user = $session->getSessionedUser();
		$friend = $user->getContacts(0, 5000);		
		if($friend->contacts->count > 0)
		{
			$aContactList = array();
			foreach($friend->contacts->contact as $oContact)
			{				
				$sEmail = $sUsername = '';
				foreach($oContact->fields as $aField)
				{
					switch($aField->type)
					{
						case 'yahooid':
							$sEmail = $aField->value .'@yahoo.com';
							break;
						case 'email':
							$sEmail = $aField->value;
							break;	
						case 'name':
							$sUsername = $aField->value->givenName . ' ' . $aField->value->middleName . ' ' . $aField->value->familyName;
							break;
					}				
					
				}
				if($sEmail != '')
				{
					if($sUsername == '')
						$sUsername = $sEmail;
					$aContactList[] = array(
						'name' => $sUsername,
					 	'email' => $sEmail 
					);
				}
			}		
			$encodeContact = urlencode(json_encode($aContactList));
			if(isset($_SESSION['callback']))
			{
				$aContactList = array();
				$sUrlCallback = $_SESSION['callback'];
				unset($_SESSION['callback']);
				$sForm ='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
    <head>
        <title></title>	
    </head>
    <body>
<form action="'.$sUrlCallback.'" method="POST" id="yahoo_connect">
<input type="hidden" name="'.$token_name.'" value="'.$sSecurityToken.'"/>
	<input name="contact" type="hidden" value="'.$encodeContact.'"/>
	
	<input name="get_success" type="hidden" value="1" />
</form>
<script language="JavaScript" type="text/javascript">
	submitYahoo();
	function submitYahoo()
	{
		document.getElementById("yahoo_connect").submit();		
	}		
</script>    </body>
</html>';

				echo $sForm;				
				exit();
			}
			echo $encodeContact;			
		}
		else
		{
			$aContactList[] = array(
						'name' => '',
					 	'email' => '',
						'errors' => 'There is no contact in your account.',	
					);
			$encodeContact = urlencode(json_encode($aContactList));
			if(isset($_SESSION['callback']))
			{
				$sUrlCallback = $_SESSION['callback'];
				unset($_SESSION['callback']);
						$sForm ='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
    <head>
        <title></title>	
    </head>
    <body>
<form action="'.$sUrlCallback.'" method="POST" id="yahoo_connect">
	<input name="contact" type="hidden" value="'.$encodeContact.'"/>
	<input type="hidden" name="'.$token_name.'" value="'.$sSecurityToken.'"/>
	<input name="get_success" type="hidden" value="1" />
</form>
<script language="JavaScript" type="text/javascript">
	submitYahoo();
	function submitYahoo()
	{
		document.getElementById("yahoo_connect").submit();		
	}		
</script></body>
</html>';
				echo $sForm;				
				exit();
			}
			echo $encodeContact;	
		}
		$_SESSION['login'] = true;	
	}
}
if(array_key_exists("logout", $_GET))
{
	YahooSession::clearSession();
	unset($_SESSION['login']);
	header("Location: index.php");
}

